<template>
  <a-flex justify="flex-end">
    <a-button @click="loadRequest(props.model.id)"> {{ $t("104") }}</a-button>
  </a-flex>
  <a-table :columns="colRequest" :data-source="lstRequest" size="small">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex == '_actions'">
        <a-button type="link" @click="onOpenChange(record)">{{
          $t("411")
        }}</a-button>
      </template>
    </template>
  </a-table>
  <a-modal v-model:open="state.popHandler" width="1024px" :footer="null">
    <a-row :gutter="8">
      <a-col :span="6">
        <RequestViewer :vr="state.vr"></RequestViewer>
        <p></p>
        <a-input
          v-model:value="state.denyHint"
          :placeholder="$t('412')"
        ></a-input>
        <p></p>
        <a-button danger @click="denyRequest()">{{ $t("413") }}</a-button>
      </a-col>
      <a-col
        v-if="
          !state.vr.acceptTs && !CommonDefine.isStringValue(state.vr.denyHint)
        "
        :span="18"
      >
        <a-flex class="ToolBar" justify="space-between">
          <strong>额外的访问权限</strong>
        </a-flex>
        <VisitRightEditor ref="refVre"></VisitRightEditor>
        <a-flex class="ToolBar" justify="space-between">
          <strong>字段限制</strong>
        </a-flex>
        <ShareVisitLimitEditor
          ref="refSrf"
          :model="props.model"
        ></ShareVisitLimitEditor>
        <a-flex class="ToolBar" justify="space-between">
          <strong>记录限制</strong>
        </a-flex>
        <FveEditor
          ref="refFvee"
          :single="false"
          :readonly="state.editMode < 1"
          :fields="appStore.allModelFieldMap.get(props.model.id)"
          :inline="true"
        ></FveEditor>
        <p>
          <a-button type="primary" @click="changeRequest()">{{
            $t("415")
          }}</a-button>
        </p>
      </a-col>
      <a-col
        v-if="
          state.vr.acceptTs && !CommonDefine.isStringValue(state.vr.denyHint)
        "
        :span="18"
      >
        <VisitGroupViewer :vg="state.vg" :show-user="true"></VisitGroupViewer>
      </a-col>
    </a-row>
  </a-modal>
</template>
<script setup>
import { nextTick, reactive, ref, watch } from "vue";
import CommonDefine from "@/component/CommonDefine";
import Utils from "@/component/Utils";
import ShareVisitLimitEditor from "@/component/visit/ShareRuleOfFieldEditor.vue";
import RequestViewer from "@/component/visit/VisitRequestViewer.vue";
import VisitGroupViewer from "@/component/visit/VisitGroupViewer.vue";
import { useI18n } from "vue-i18n";
import VisitRightEditor from "@/component/visit/ShareRuleOfModelEditor.vue";
import FveEditor from "@/component/query/FveEditor.vue";

const { t } = useI18n();
const appStore = CommonDefine.appStore();
const props = defineProps({ model: Object });
const colRequest = [
  { title: t("416"), dataIndex: "uname" },
  { title: t("417"), dataIndex: "requestTs" },
  { title: t("418"), dataIndex: "acceptTs" },
  { title: t("419"), dataIndex: "handle" },
  { title: "", dataIndex: "_actions" },
];
const state = reactive({
  vr: {},
  vg: {},
  popHandler: false,
  denyHint: "",
});
const refFvee = ref();
const refSrf = ref();
const refVre = ref();
const lstRequest = reactive([]);
const lstGroup = reactive([]);

const loadRequest = (modelId) => {
  loadGroup(modelId);
  CommonDefine.clearArray(lstRequest);
  CommonDefine.apiGetArm(`/api/v1/share/requests/${modelId}`, (d) => {
    d.forEach((x) => {
      x.uname = Utils.getUserName(x.requestUid);
      if (!CommonDefine.isStringValue(x.acceptTs)) {
        x.handle = t("420");
      } else {
        if (!CommonDefine.isStringValue(x.denyHint)) {
          x.handle = t("415");
        } else {
          x.handle = t("413");
        }
      }
      lstRequest.push(x);
    });
  });
};

const changeRequest = () => {
  state.vr.filters = refFvee.value.getFves();
  state.vr.rights = refVre.value.getRules();
  state.vr.limits = refSrf.value.getRules();
  CommonDefine.apiPostArm("/api/v1/share/change-request", state.vr, (d) => {
    state.vg = d;
    CommonDefine.alert(t("421"));
    loadRequest(props.model.id);
  });
};

const denyRequest = () => {
  let pd = {
    modelId: state.vr.modelId,
    requestUid: state.vr.requestUid,
    denyHint: state.denyHint,
  };
  CommonDefine.apiPostArm("/api/v1/share/change-request", pd, (d) => {
    CommonDefine.alert(t("421"));
    loadRequest(props.model.id);
  });
};

const onOpenChange = (vr) => {
  state.vr = vr;
  state.denyHint = vr.denyHint;
  if (
    CommonDefine.isStringValue(vr.acceptTs) &&
    !CommonDefine.isStringValue(vr.denyHint)
  ) {
    // 已经分配了访问组，要把访问组的信息展示出来
    state.vg = lstGroup.find((x) => x.uId == vr.requestUid);
    state.vg.acceptTs = vr.acceptTs;
  }
  state.popHandler = true;
  if (!CommonDefine.isStringValue(state.vr.denyHint)) {
    nextTick(() => {
      if (!CommonDefine.isStringValue(state.vr.acceptTs)) {
        refVre.value.setRules(vr.rights);
      }
    });
  }
};

const loadGroup = (modelId) => {
  CommonDefine.clearArray(lstGroup);
  CommonDefine.apiGetArm(`/api/v1/share/vgs/${modelId}`, (d) => {
    d.forEach((x) => {
      lstGroup.push(x);
    });
  });
};

watch(
  () => props.model,
  (newValue) => {
    loadRequest(newValue.id);
  },
  {
    immediate: true,
  }
);
</script>
